package hongcaosp.app.android.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blackchopper.loginshare.impl.LoginShare;
import com.blackchopper.loginshare.interfaces.OnLoginshareListener;
import com.blackchopper.loginshare.model.Type;

import hongcaosp.app.android.MainActivity;
import hongcaosp.app.android.R;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.utils.TaskPool;

public class LoginActivity extends BaseActivity implements ICodeView,ILoginView ,OnLoginshareListener {
    private EditText phoneET;
    private EditText codeET;
    private TextView codeBTN;
    private LoginPresenter loginPresenter;
    private LoginShare loginShare;

    @Override
    protected void initData(Intent intent) {

    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);

        findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

//        findViewById(R.id.password_login_btn).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LoginActivity.this,PasswordLoginActivity.class);
//                startActivity(intent);
//            }
//        });
//        findViewById(R.id.tv_register).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
//                startActivity(intent);
//            }
//        });

        findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginAction();
            }
        });

        phoneET = findViewById(R.id.phone_et);
        codeET = findViewById(R.id.code_et);
        codeBTN = findViewById(R.id.code_btn);
        loginShare = new LoginShare(this);
        loginShare.register(this);

        findViewById(R.id.btn_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginShare.launchWechatLogin();
            }
        });

        codeBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                codeAction();
            }
        });

        loginPresenter = new LoginPresenter(this,this);
    }

    private void codeAction() {
        String phone = phoneET.getText().toString();
        if (phone.length() < 11) {
            ToastManager.getInstance().showToast("手机号长度不够");
            return;
        }
        codeBTN.setEnabled(false);
        loginPresenter.sendCode(phone);
    }

    private void loginAction() {
        String phone = phoneET.getText().toString();
        String code = codeET.getText().toString();
        if (phone.length() < 11) {
            ToastManager.getInstance().showToast("手机号长度不够");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastManager.getInstance().showToast("请输入验证码");
            return;
        }
        loginPresenter.loginWithCode(phone,code);
    }

    private void codeCount(int count) {
        final int n = count - 1;
        if (n >= 0) {
            codeBTN.setText(n + "S" );
            TaskPool.myPool().postTaskOnMainDelay(new Runnable() {
                @Override
                public void run() {
                    codeCount(n);
                }
            },1000);
        } else {
            codeBTN.setEnabled(true);
            codeBTN.setText("获取验证码");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginShare.unRegister();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_login;
    }

    public static void toLogin(Context activity) {
        if (activity != null) {
            Intent intent = new Intent(activity,LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void onCodeSend(boolean success) {
        if (success) {
            codeCount(60);
        } else {
            codeBTN.setEnabled(true);
        }
    }

    @Override
    public void onLogged(boolean isOk, String message) {
        if (isOk) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } else {
            ToastManager.getInstance().showToast(message);
        }
    }

    @Override
    public void onLoginSuccess(Type type, String code, String info) {
        loginPresenter.loginWithWechat(info);
    }

    @Override
    public void onLoginCancel(Type type) {
        ToastManager.getInstance().showToast("授权取消");
    }

    @Override
    public void onLoginError(Type type, int errorCode) {
        ToastManager.getInstance().showToast("授权失败");
    }

    @Override
    public void onShareSuccess(Type type) {

    }

    @Override
    public void onShareCancel(Type type) {

    }

    @Override
    public void onShareError(Type type, int code) {

    }
}
